package com.idis.entity;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.idis.common.dictionary.UserRole;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ming on 2017/12/26.
 */
@Entity
@JsonDeserialize(as = User.class)
public class User implements UserDetails {

    @Id
    @GeneratedValue
    private Integer id;
    @Column
    private String username;
    @Column
    private String password;
    @Column
    private Integer role;
    @Column
    private String phone;
    @Transient
    List authorities;
    @Transient
    boolean accountNonExpired;
    @Transient
    boolean accountNonLocked;
    @Transient
    boolean credentialsNonExpired;
    @Transient
    boolean enabled;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public Integer getRole() {
        return role;
    }

    public void setRole(Integer role) {
        this.role = role;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public List<com.idis.entity.SimpleGrantedAuthority> getAuthorities() {
        if(role==null)
            role=0;
        authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_" + UserRole.getRole(role)));
        return authorities;
    }

}

